<template>
	<el-dialog v-model="show" title="切换人员组织" width="400" :close-on-click-modal="false" :close-on-press-escape="false">
		<el-form label-width="auto">
			<el-form-item label="选择组织">
				<el-cascader :options="trees" ref="groupCascaderRef" v-model="group_id" :props="props"
					placeholder="请选择人员组织" style="width:100%;" clearable />
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="show = false; group_id = null; trees = []">取消</el-button>
				<el-button type="primary" @click="onSubmit">确认</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { fetchUserGroup, fetchUserSwitch } from '@/api/user.services'
import { ElMessage } from 'element-plus'
const emits = defineEmits(['success'])
const group_id = ref(null)
const trees = ref([])
const show = ref(false)
const props = {
	label: "name",
	value: "id",
	children: "children",
	checkStrictly: true,
	emitPath: false
}
const initGroups = async () => {
	const result = await fetchUserGroup()
	if (result.code == 1) {
		trees.value = result.data[0].children
	}
}
onMounted(() => {
	initGroups()
})
const ids = ref([])
const open = (row) => {
	group_id.value = null
	ids.value = row
	show.value = true
}
defineExpose({
	open
})
const onSubmit = async () => {
	const params = {
		ids: ids.value,
		group_id: group_id.value
	}
	const result = await fetchUserSwitch(params)
	if (result.code == 1) {
		ElMessage.success('切换成功')
		emits('success')
		show.value = false
		group_id.value = null
		ids.value = []
	}
}
</script>

<style lang="scss" module></style>
